package com.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.Constants.RedisScontants;
import com.power.domain.Category;
import com.power.mapper.CategoryMapper;
import com.power.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@CacheConfig(cacheNames = "com.power.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{
@Autowired
private CategoryMapper categoryMapper;
    @Override
    public Page pageList(Page<Category> page, String categoryname, Integer status) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (StringUtils.hasText(categoryname))
        {
            lambdaQueryWrapper.eq(Category::getCategoryName,categoryname);
        }
        if (status!=null)
        {
            lambdaQueryWrapper.eq(Category::getStatus,status);
        }

        Page<Category> page1 = categoryMapper.selectPage(page, lambdaQueryWrapper);
        return page1;
    }

    @Override
    @Cacheable(key = RedisScontants.ALL_CATEGORY)
    public List<Category> getCategorytable() {
        List<Category> categories = categoryMapper.selectList(null);
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        for (Category category : categories) {
            Long categoryId = category.getCategoryId();
            lambdaQueryWrapper.eq(Category::getParentId,categoryId);
            List<Category> list = categoryMapper.selectList(lambdaQueryWrapper);
            category.setList(list);

        }
        return categories ;
    }

    @Override
    public List<Category> listCategory() {
        return null;
    }
}
